package com.example.service;

import com.example.dao.ListRepository;
import com.example.entity.TodoList;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ListService {

    @Resource
    private ListRepository listRepository;

    public void save(TodoList todolist) {
        listRepository.save(todolist);
    }

    public void delete(Long id) {
        listRepository.deleteById(id);
    }

    public TodoList findById(Long id) {
        return listRepository.findById(id).orElse(null);
    }

    public List<TodoList> findAll() {
        return listRepository.findAll();
    }

    public Page<TodoList> findPage(Integer pageNum, Integer pageSize, String name) {
        // 构建分页查询条件
        Sort sort = new Sort(Sort.Direction.ASC, "is_finished");
        Pageable pageRequest = PageRequest.of(pageNum - 1, pageSize, sort);
        return listRepository.findByNameLike(name, pageRequest);
    }
}

